package com.example.blackbox.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.blackbox.Data.News;
import com.example.blackbox.DataBase.OpDataBase;
import com.example.blackbox.Net.NetTool;
import com.example.blackbox.R;



public class PageActivity extends Activity {
    ImageView banner,head,collections;
    TextView title,author,time,level,info,message,content;
    News news;
    String infoString;
    Handler handler;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page);
        initUI();
        final Bundle data=getIntent().getExtras();
        news=(News)data.get("news");


        Glide.with(this).load(news.getImgUrl()).into(banner);
        Glide.with(this).load(news.getAuthorHead()).into(head);

        title.setText(news.getNewsTitle());
        author.setText(news.getAuthor());
        time.setText(news.getNewsDate());
        level.setText("LV."+news.getLevel());
        info.setText("评论（已有"+news.getNewsMessage()+"条评论）");

        new Thread(new Runnable() {
            @Override
            public void run() {
                infoString = NetTool.getInfoString(news.getInfo());
                Message m=handler.obtainMessage();
                handler.sendMessage(m);
            }
        }).start();

        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                content.setText(infoString);
            }
        };

        collections.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collections.setImageBitmap(Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.collections)));
                SharedPreferences sp=getSharedPreferences("userinfo", Context.MODE_PRIVATE);
                String num=sp.getString("collections","0");
                int temp=Integer.parseInt(num);
                temp=temp+1;
                Log.d("tests",num);
                sp.edit().putString("collections",String.valueOf(temp)).commit();

                OpDataBase dataBase=new OpDataBase(PageActivity.this);
                dataBase.add(news);
            }
        });

    }
    public void initUI(){
        banner=(ImageView)findViewById(R.id.page_iv_banner);
        collections=(ImageView)findViewById(R.id.page_iv_collections);
        head=(ImageView)findViewById(R.id.page_iv_head);
        title=(TextView)findViewById(R.id.page_tv_title);
        author=(TextView)findViewById(R.id.page_tv_author);
        time=(TextView)findViewById(R.id.page_tv_time);
        level=(TextView)findViewById(R.id.page_tv_level);
        info=(TextView)findViewById(R.id.page_tv_info);
        message=(TextView)findViewById(R.id.page_tv_message);
        content=(TextView)findViewById(R.id.page_sc_content);
    }
}
